//
//  NYMyClassListViewController.m
//  NiuWaJiaoYu
//
//  Created by MinLison on 2018/1/22.
//  Copyright © 2018年 niuwaketang. All rights reserved.
//

#import "NYMyClassListViewController.h"
#import "NYCourseViewController.h"
#import "NYDelSubjectRequest.h"
#import "NYMyClassesRequest.h"
#import "NYRefreshFooter.h"

@interface NYMyClassListViewController ()
@property(nonatomic, strong) QMUIButton *schoolButton;
@property(nonatomic, strong) QMUIPopupMenuView *popMenuView;
@property(nonatomic, assign) NYMyClassesType choseClassesType;
@property(nonatomic, strong) NYMyClassesRequest *request;
@end

@implementation NYMyClassListViewController


- (void)viewDidLoad {
        [super viewDidLoad];
        self.choseClassesType = NYMyClassesTypeKnowledgePoint;
        [self loadData];
}
- (void)loadData
{
        [self setLoading:YES animation:YES];
        [self configNimbus];
        self.request = [NYMyClassesRequest requestWithParams:@{
                                                               kRequestKeyTypeID : @(self.classesType)
                                                               }];
        [[self.request pullDown] startWithSuccess:^(__kindof BaseRequest * _Nonnull request, __kindof NSArray<NYMyClassesModel *> * _Nonnull data) {
                [self.tableViewModel addObjectsFromArray:data];
                [self.tableView reloadData];
                [self setSuccess];
        } failed:^(__kindof BaseRequest * _Nonnull request, NSError * _Nonnull error) {
                [self setError:error];
        }];
}
- (void)setSuccess
{
        [super setSuccess];
        [self configRefreshFooter];
}
- (void)configRefreshFooter
{
        @weakify(self);
        self.tableView.mj_footer = [NYRefreshFooter footerWithRefreshingBlock:^{
                @strongify(self);
                [[self.request pullUp] startWithSuccess:^(__kindof BaseRequest * _Nonnull request, __kindof NSArray<NYMyClassesModel *> * _Nonnull data) {
                        [self.tableViewModel addObjectsFromArray:data];
                        [self.tableView reloadData];
                        data.count == 0 ? [self.tableView.mj_footer endRefreshingWithNoMoreData] : [self.tableView.mj_footer endRefreshing];
                } failed:^(__kindof BaseRequest * _Nonnull request, NSError * _Nonnull error) {
                        [self.tableView.mj_footer endRefreshingWithNoMoreData];
                }];
        }];
}
- (void)configNavigationBar:(BaseNavigationBar *)navigationBar
{
        [super configNavigationBar:navigationBar];
        self.title = @"我的课程";
}
- (void)configNimbus {
        [super configNimbus];
        
        self.tableViewModel.delegate = self;
        @weakify(self);
        [self.tableViewActions attachToClass:[NYMyClassesModel class] tapBlock:^BOOL(NYMyClassesModel *object, NYMyClassListViewController * target, NSIndexPath *indexPath) {
                
                @strongify(self);
                [self.navigationController pushViewController:[[NYCourseViewController alloc] initWithDetailContentStatus:target.classesType withId:object.class_id] animated:true];
                return true;
        }];
}
- (void)delClass:(NYMyClassesModel *)object atIndexPath:(NSIndexPath *)indexPath
{
        [[NYDelSubjectRequest requestWithType:(NYDelObjectTypeClasses) param:@{
                                                                               @"class_record_id" : NOT_NULL_STRING_DEFAULT_EMPTY(object.id)
                                                                               }] startWithSuccess:^(__kindof BaseRequest * _Nonnull request, __kindof NSString * _Nonnull data) {
                [NYTipClass showText:request.tipString inView:self.view];
        } failed:^(__kindof BaseRequest * _Nonnull request, NSError * _Nonnull error) {
                [NYTipClass showErrorWithText:error.localizedDescription inView:self.view];
                [self.tableViewModel insertObject:object atRow:indexPath.row inSection:indexPath.section];
                [self.tableView reloadData];
        }];
}
/// MARK: - TableViewModel Delegate
- (BOOL)tableViewModel:(NIMutableTableViewModel *)tableViewModel
         canEditObject:(id)object
           atIndexPath:(NSIndexPath *)indexPath
           inTableView:(UITableView *)tableView
{
        return YES;
}
- (UITableViewRowAnimation)tableViewModel:(NIMutableTableViewModel *)tableViewModel
              deleteRowAnimationForObject:(id)object
                              atIndexPath:(NSIndexPath *)indexPath
                              inTableView:(UITableView *)tableView
{
        [self delClass:(NYMyClassesModel *)object atIndexPath:(NSIndexPath *)indexPath];
        return UITableViewRowAnimationAutomatic;
}

+ (NYViewControllerNavigationBarType)navigationBarType
{
        return NYViewControllerNavigationBarTypeBlue;
}

@end
